﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SlamCms.SharePoint.Migration
{
	public class MigratableList : IMigratable
	{
		public List<MigratableListItem> ListItems { get; set; }
		public List<MigratableListDependency> ListDependencies { get; set; }

		public MigratableList()
		{
			this.Fields = new MigratableFieldList();
			this.ContentTypes = new MigratableContentTypeList();
			this.Views = new List<MigratableView>();
			this.ListItems = new List<MigratableListItem>();
			this.ListDependencies = new List<MigratableListDependency>();
		}

		public MigratableContentTypeList ContentTypes { get; set; }
		public MigratableFieldList Fields { get; set; }
		public List<MigratableView> Views { get; set; }

		public string Id { get; set; }
		public string Title { get; set; }
		public string Description { get; set; }
		public int ListTemplateId { get; set; }
		public bool ContentTypesEnabled { get; set; }
		public bool EnableVersioning { get; set; }
		public bool EnableModeration { get; set; }
		public bool OnQuickLaunch { get; set; }

		#region Serialization

		public XElement ToXml()
		{
			XElement node = new XElement("List");
			node.Add(new XAttribute("Id", Id));
			node.Add(new XAttribute("Title", Title));
			node.Add(new XAttribute("Description", Description));
			node.Add(new XAttribute("ContentTypesEnabled", ContentTypesEnabled));
			node.Add(new XAttribute("EnableVersioning", EnableVersioning));
			node.Add(new XAttribute("EnableModeration", EnableVersioning));
			node.Add(new XAttribute("OnQuickLaunch", OnQuickLaunch));
			node.Add(new XAttribute("ListTemplateId", ListTemplateId));
			//node.Add(ListDependneciesToXml());
			node.Add(Fields.ToXml());
			node.Add(ContentTypes.ToXml());
			node.Add(ViewsToXml());
			node.Add(ListItemsToXml());
			return node;
		}

		public void FromXml(XElement element)
		{
			this.Id = element.Attribute("Id").Value;
			this.Title = element.Attribute("Title").Value;
			this.Description = element.Attribute("Description").Value;
			this.ContentTypesEnabled = bool.Parse(element.Attribute("ContentTypesEnabled").Value);
			this.EnableVersioning = bool.Parse(element.Attribute("EnableVersioning").Value);
			this.EnableModeration = bool.Parse(element.Attribute("EnableModeration").Value);
			this.OnQuickLaunch = bool.Parse(element.Attribute("OnQuickLaunch").Value);
			this.ListTemplateId = int.Parse(element.Attribute("ListTemplateId").Value);
			this.Fields.FromXml(element);
			this.ContentTypes.FromXml(element);
			ViewsFromXml(element);
			ListItemsFromXml(element);
			//ListDependenciesFromXml(element);
		}

		private XElement ListDependneciesToXml()
		{
			XElement node = new XElement("ListDependencies");
			foreach (var list in this.ListDependencies)
			{
				node.Add(list.ToXml());
			}
			return node;
		}

		private void ListDependenciesFromXml(XElement element)
		{
			XElement listDependenciesElement = element.Element("ListDependencies");
			if (listDependenciesElement.HasElements)
			{
				foreach (XElement listDependencyElement in listDependenciesElement.Elements())
				{
					MigratableListDependency listDependency = new MigratableListDependency();
					listDependency.FromXml(listDependencyElement);
					ListDependencies.Add(listDependency);
				}
			}
		}

		private XElement ListItemsToXml()
		{
			XElement node = new XElement("ListItems");
			foreach (var listItem in this.ListItems)
			{
				node.Add(listItem.ToXml());
			}
			return node;
		}

		private void ListItemsFromXml(XElement element)
		{
			XElement listItemsElement = element.Element("ListItems");
			if (listItemsElement.HasElements)
			{
				foreach (XElement listItemElement in listItemsElement.Elements())
				{
					MigratableListItem listItem = new MigratableListItem();
					if (listItemElement.Name == "DocumentListItem")
					{
						listItem = new MigratableDocumentListItem();
					}
					else if (listItemElement.Name == "FolderListItem")
					{
						listItem = new MigratableFolderListItem();
					}
					listItem.FromXml(listItemElement);
					this.ListItems.Add(listItem);
				}
			}
		}

		private XElement ViewsToXml()
		{
			XElement node = new XElement("Views");
			foreach (MigratableView v in this.Views)
			{
				node.Add(v.ToXml());
			}
			return node;
		}

		private void ViewsFromXml(XElement element)
		{
			XElement listsNode = element.Element("Views");
			if (listsNode.HasElements)
			{
				foreach (XElement child in listsNode.Elements())
				{
					MigratableView view = new MigratableView();
					view.FromXml(child);
					this.Views.Add(view);
				}
			}
		}

		#endregion
	}


}